package cn.jinjimi.app.custom.utils;

import android.content.Context;
import android.view.View;

import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.ui.activity.CustomEditOrAddActivity;
import cn.jinjimi.app.custom.ui.dialog.PhonesSelectDialog;
import cn.jinjimi.app.store.entity.custom.ContactInfo;
import cn.jinjimi.app.main.dialog.CommAlertDialog;

import java.util.List;

/**
 * 拨打客户的电话
 * <p/>
 * Created by jinsen on 15/11/11.
 */
public class CustomContactHelper {

    private int actionType;

    public static final int TypeCall = 0;
    public static final int TypeMsg = 1;

    Context mContext;
    Custom mCustom;

    public CustomContactHelper(Context context, Custom custom, int type) {
        mContext = context;
        mCustom = custom;
        actionType = type;
    }

    public void contact() {
        if (mCustom == null)
            return;

        List<ContactInfo> contactInfos = mCustom.getContactInfos();

        if (contactInfos == null || contactInfos.isEmpty()) {
            didNotSetPhone();
        } else {
            hasSetPhone(contactInfos);
        }
    }

    private void didNotSetPhone() {
        final CommAlertDialog dialog = new CommAlertDialog(mContext);
        dialog.setTitle("手机号码为空")
                .setMessage("是否前往客户编辑页填写")
                .setRightBtn(mContext.getString(R.string.ok), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismissDialog();
                        CustomEditOrAddActivity.launch(mContext, mCustom, true);
                    }
                });
        dialog.setCanceledOnTouchOutside(false);
        dialog.showDialog(CommAlertDialog.Style.TWO_BUTTON);
    }

    private void hasSetPhone(List<ContactInfo> contactInfos) {
        if (contactInfos.size() == 1) {
            switch (actionType) {
                case TypeCall:
                    AndroidUtils.call(mContext, contactInfos.get(0).getValue());
                    break;
                case TypeMsg:
                    AndroidUtils.sendMsg(mContext, contactInfos.get(0).getValue(), "");
                    break;
            }
        } else {
            PhonesSelectDialog phonesSelectDialog = new PhonesSelectDialog(mContext, mCustom, actionType);
            phonesSelectDialog.setCanceledOnTouchOutside(true);
            phonesSelectDialog.show();
        }
    }
}
